package com.cn.demo.service.impl;

import com.cn.demo.entity.*;
import com.cn.demo.mapper.*;
import com.cn.demo.service.PageInfoUtilServer;
import com.cn.demo.service.StudentService;
import com.cn.demo.utils.Result;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private PageInfoUtilServer pageInfoUtilServer;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private CurriculumMapper curriculumMapper;


    @Autowired
    private TeachingMapper teachingMapper;

    @Autowired
    private ExperimentMapper experimentMapper;

    @Autowired
    private InternshipMapper internshipMapper;

    @Autowired
    private InternshipLogMapper internshipLogMapper;

    @Autowired
    private DesignMapper designMapper;

    @Autowired
    private DesignTeamMapper designTeamMapper;

    @Autowired
    private EvaluateMapper evaluateMapper;

    @Override
    public Result add(Student student) {
        if(studentMapper.getByStudentId(student.getStudentId())!= null){
            return Result.error("学号存在");
        }
        student.setPassword("123456");
        if (studentMapper.insert(student)>0){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result get(int page,int limit ,Student student) {
        PageHelper.startPage(page,limit);
        List<Student> studentList = studentMapper.get(student);
        return Result.success(pageInfoUtilServer.currency(studentList));
    }

    @Override
    public Result getByStudentId(String studentId) {
        Student student = studentMapper.getByStudentId(studentId);
        return Result.success(student);
    }

    @Override
    public Result delete(String[] studentIdList) {
        for (String studentId : studentIdList) {
            Curriculum curriculum = new Curriculum();
            List<Curriculum> curriculumList = curriculumMapper.getByStudentId(curriculum, studentId);
            for (Curriculum curriculumItem : curriculumList) {
                //删除实验
                experimentMapper.delete(curriculumItem.getCurriculumExperimentId(),studentId);
                evaluateMapper.delete(curriculumItem.getCurriculumExperimentId(),studentId);
                //删除实训
                Internship internship = internshipMapper.getInternshipCorporation(studentId, curriculumItem.getCurriculumInternshipId());
                if (internship!=null){
                    internshipLogMapper.delete(internship.getInternshipId());
                    internshipMapper.deleteByPrimaryKey(internship.getInternshipId());
                }
                evaluateMapper.delete(curriculumItem.getCurriculumInternshipId(),studentId);
                //删除课程设计
                Design designSubject = designMapper.getDesignSubject(studentId, curriculumItem.getCurriculumDesignId());
                if(designSubject!=null){
                    List<DesignTeam> designTeamList = designSubject.getDesignTeamList();
                    designTeamMapper.delete(designSubject.getDesignId(),studentId);
                    evaluateMapper.delete(curriculumItem.getCurriculumDesignId(),studentId);
                    if (designTeamList.size()==1){
                        //没有人直接删除设计
                        designMapper.deleteByPrimaryKey(designSubject.getDesignId());
                    }
                }
                //删除班级学生
                teachingMapper.organizationStudentDeleteOne(studentId);
            }
        }
      if (studentMapper.delete(studentIdList)>0){
          return Result.success();
      }
      return Result.error();
    }

    @Override
    public Result update(Student student) {
        if (studentMapper.getByStudentId(student.getStudentId())==null) {
            return Result.error("学生不存在");
        }
        if (studentMapper.update(student)>0){
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 修改密码
     * @param studentId
     * @param oldPassword
     * @param affirmPassword
     * @return
     */
    @Override
    public Result modifyPassword(String studentId, String oldPassword, String affirmPassword) {
        if (studentMapper.checkInfo(studentId, oldPassword) != null) {
            if (studentMapper.updatePassword(studentId, affirmPassword)>0) {
                return Result.success();
            }
            return Result.error();
        }
        return Result.error("密码错误");
    }

    @Override
    public Result login(Student student) {
        Student login = studentMapper.login(student);
        if (login == null) {
            return Result.error("用户名或密码错误");
        } else {
            String token = UUID.randomUUID() + "";
            login.setToken(token);
            if (studentMapper.updateToken(token, login.getStudentId()) == 1) {
                return  Result.success(login);
            } else {
                return  Result.error();
            }
        }
    }
}